package com.zhuchang.self_rpc.test.democoding.nettydemo;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @Author RenPu
 * @Date 2022/2/9 11:25
 * @Version 1.0
 * @Description: 编写NettyClient的编码
 **/
public class NettyClient {

    public static void main(String[] args) {

        //创建循环线程组
        EventLoopGroup eventExecutors = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyClientHandler())
                    .handler(new ChannelInitializer<SocketChannel>(){
                        @Override
                        protected void initChannel(SocketChannel channel) throws Exception {
                            channel.pipeline().addLast(new NettyClientHandler());
                        }
                    });

            System.out.println("客户端 is ok ......");

            ChannelFuture future = bootstrap.connect(new InetSocketAddress("127.0.0.1", 8688)).sync();

            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            System.out.println("客户端启动异常.....");
            e.printStackTrace();
        } finally {
            eventExecutors.shutdownGracefully();
        }




    }


}
